﻿using Model;
using UnityEngine;

namespace Core.Mgr.ABTest
{
    /// <summary>
    /// AB测试管理类
    /// </summary>
    public static class AbTestMgr
    {
        /// 当前的测试组
        public static TestGroupInfo CurTestGroup { get; private set; } = new();

        /// <summary>
        /// 当前测试组的ID
        /// </summary>
        public static string CurTestGroupId => CurTestGroup.TestId;

        public static void OnInit(ConfigModel configModel)
        {
            //进行ABTest种类划分,走正常的allTestGroup分组
            if (configModel.allTestGroup != null && configModel.allTestGroup.Count > 0 &&
                !string.IsNullOrEmpty(configModel.abTestName))
            {
                string abTestTypeKey = $"ABTestType_{configModel.abTestName}";
                string testId = PlayerPrefs.GetString(abTestTypeKey);
                if (string.IsNullOrEmpty(testId)) //之前没有进行过分组
                {
                    CurTestGroup = AbRandomTool.RandomGroup(configModel.allTestGroup);
                    PlayerPrefs.SetString(abTestTypeKey, CurTestGroup.TestId);
                    // 用户属性打点
                    DaDianMgr.SetUserProperty(configModel.abTestName, CurTestGroup.TestId, false);
                    Debug.Log($"ABTest分类完成，Type={CurTestGroup.TestId}");
                }
                else
                {
                    CurTestGroup = configModel.allTestGroup.Find(item => item.TestId.Equals(testId));
                    Debug.Log($"之前已进行过ABTest分组，Type={CurTestGroup.TestId}");
                }
            }
        }
    }
}